﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Aspose.Words;
using Aspose.Slides;
using Aspose.Cells;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;

public partial class admin_SeeNews : System.Web.UI.Page
{
    private static string n_category;//保存查询类型，1为关键字、2为全部
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["n_category"]))
        {
            if (Session["name"] == null)
            {
                Response.Write("<script>alert('非法登录!!!');window.top.location='../Login.aspx';</script>");
                return;
            }
            else
            {
                BindData();
            }

        }
        else
        {
            Response.Redirect("Main.aspx");
        }


    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "")
        {
            Response.Write("<script>alert('请输入标题');</script>");
            return;
        }
        else
        {
            string[] path = saveFile();
            //data.dt_pptPath = path[0];
            //data.dt_VideoPath = "0";
            //DALData.Insert(data);

            string dt_pptPath = path[0];
            string dt_VideoPath = "0";

            string insersql = "insert into tb_News(n_Title,n_Time,n_Category,n_ppt,n_video) values(@n_Title,@n_Time,@n_Category,@n_ppt,@n_video)";
            OleDbParameter[] param = { 
                                     new OleDbParameter("@n_Title",TextBox1.Text.ToString()),
                                     new OleDbParameter("@n_Time",DateTime.Now.ToString()),
                                     new OleDbParameter("@n_Category",n_category),
                                      new OleDbParameter("@n_ppt",dt_pptPath),
                                       new OleDbParameter("@n_video",dt_VideoPath)
                                 };
            int flag = OleDbHelper.ExecuteNonSql(insersql, param);
            if (flag > 0)
            {
                Response.Write("<script>alert('上传成功')</script>");
                BindData();
            }
            else
            {
                Response.Write("<script>alert('上传失败，请稍候再试')</script>");
                return;
            }
            
           
        }
      
    }

    private string[] saveFile()
    {
        string path = "file/office/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
        string path2 = "file/Video/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
        string ArticlePath = Server.MapPath("~/") + path;
        string ArticlePath2 = Server.MapPath("~/") + path2;

        Pdf2Swf pdf2swf = new Pdf2Swf();
        //建立文件主文件夹
        if (!Directory.Exists(ArticlePath))
        {
            Directory.CreateDirectory(ArticlePath);
        }
        if (!Directory.Exists(ArticlePath2))
        {
            Directory.CreateDirectory(ArticlePath2);
        }
        string saveName = Path.GetFileName(FileUpload1.FileName);  //文件名

        string extension = Path.GetExtension(FileUpload1.FileName).ToLower(); //扩展名
        saveName = saveName.Substring(0, saveName.Length - extension.Length);
        string time = DateTime.Now.ToString("HH-mm-ss");
        string fileName = time + extension; //新的文件名2

        string saveName2 = Path.GetFileName(FileUpload1.FileName);  //文件名
        string extension2 = Path.GetExtension(FileUpload1.FileName).ToLower(); //扩展名
        saveName2 = saveName2.Substring(0, saveName2.Length - extension2.Length);
        string fileName2 = time + extension2; //新的文件名2

        ArticlePath += fileName;
        ArticlePath2 += fileName2;
        //保存文件
        FileUpload1.SaveAs(ArticlePath);
        FileUpload1.SaveAs(ArticlePath2);  //保存video
        string pdfpath = ArticlePath.Substring(0, ArticlePath.Length - extension.Length) + ".pdf";
        string swfpath = ArticlePath.Substring(0, ArticlePath.Length - extension.Length) + ".swf";

        if (extension == ".doc" || extension == ".docx")
        {
            Document doc = new Document(ArticlePath);
            doc.Save(pdfpath, Aspose.Words.SaveFormat.Pdf);

            // office2pdf.DOCConvertToPDF(ArticlePath, pdfpath);
            pdf2swf.PDFConvertToSWF(pdfpath, swfpath);
        }
        else if (extension == ".ppt" || extension == ".pptx")
        {
            Presentation pres = new Presentation(ArticlePath);
            pres.SaveToPdf(pdfpath);
            pdf2swf.PDFConvertToSWF(pdfpath, swfpath);
            // pdf2swf.PDFConvertToSWF("G:/doc.pdf", " G:/1.swf");
        }
        else if (extension == ".xls" || extension == ".xlsx")
        {
            Workbook book = new Workbook(ArticlePath);
            book.Save(pdfpath, Aspose.Cells.SaveFormat.Pdf);
            //office2pdf.XLSConvertToPDF(ArticlePath, pdfpath);
            pdf2swf.PDFConvertToSWF(pdfpath, swfpath);
        }
        else if (extension == ".pdf")
        {
            pdf2swf.PDFConvertToSWF(ArticlePath, swfpath);
        }
        return new string[] { path + fileName, path2 + fileName2 };
    }

    //数据绑定
    public void BindData()
    {
        string str = Request.QueryString["n_category"];
        if (str == "grjj")
        {
            n_category = "个人简介";
        }
        else if (str == "xwdt")
        {
            n_category = "新闻动态";
        }
        else if (str == "hjzs")
        {
            n_category = "获奖证书";
        }
        else if (str == "jylw")
        {
            n_category = "教研论文";
        }
        else if (str == "cbjc")
        {
            n_category = "出版教材";
        }
        else if (str == "kycg")
        {
            n_category = "科研成果";
        }
        else if (str == "jxxd")
        {
            n_category = "教学心得";
        }
        else if (str == "tjb")
        {
            n_category = "推荐表";
        }
        else if (str == "lxfs")
        {
            n_category = "联系方式";
        }
        else if (str == "xmcl")
        {
            n_category = "项目材料";
        }
        else if (str == "kylw")
        {
            n_category = "科研论文";
        }
        else
        {
            n_category = "发明专利";
        }

        string sqlSelect = "";
        DataTable dt = new DataTable();
        sqlSelect = "select * from tb_News where n_category=@n_category";
        OleDbParameter[] param = { 
                                      new OleDbParameter("@n_category",n_category),
                                 };
        dt = OleDbHelper.GetDataTable(sqlSelect,param);
        gv_majorinfo.DataSource = dt;
        gv_majorinfo.DataBind();
    }
    //删除新闻
    protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        LinkButton delete = (LinkButton)sender;
        string sqlDelete = "delete from tb_News where n_id=" + delete.CommandArgument;
        int flag = OleDbHelper.ExecuteNonSql(sqlDelete);
        if (flag > 0)
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除成功！');</script>");
            BindData();
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除失败，请稍后重试！');</script>");
            return;
        }
    }

    //添加新闻
    protected void tjNews_Click(object sender, EventArgs e)
    {

    }

    //分页
    protected void gv_majorinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv_majorinfo.PageIndex = e.NewPageIndex;
        BindData();
    }
}